package com.spirng.leetcode.day06;

import java.util.LinkedList;
import java.util.Queue;

public class Test111 {
    int a=0;
    int b=0;
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1,null,new TreeNode(3,null,new TreeNode(4)));
        Test111 test111 = new Test111();
        int i = test111.minDepth1(root);
        System.out.println(i);
    }
    public int minDepth(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        int min=1;
        if(root==null){
            return 0;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                if(node.left!=null) queue.add(node.left);
                if(node.right!=null) queue.add(node.right);
                if(node.left==null&&node.right==null){
                    return min;
                }
            }
            min++;
        }
        return 0;
    }
    public int minDepth1(TreeNode node) {
        if(node==null){
            return 0;
        }
        if(node.left==null&&node.right==null){
            return 1;
        }
        if(node.left!=null){
            a=minDepth1(node.left)+1;
        }
        if(node.right!=null){
            b=minDepth1(node.right)+1;
        }
        if(a==0){
            return b;
        }
        if(b==0){
            return a;
        }
        return Math.min(a,b);
    }


}
